#!/usr/bin/php
<?php
$mysql_host = "remote.host.com";
$mysql_username = "accel";
$mysql_username = "accel";
$mysql_db = "users";

function addmessage($msg) {
    $fh=fopen("/var/www/dumps/checklog","a+");
    if (!$fh)
        die ("Cannot open log!");
    fwrite($fh,"[".date("h:i:s D:m:Y")."] ".$msg."\n");
    fclose($fh);
    print $msg."\n";
    }

addmessage("Get sessions");
    
exec("/var/www/scripts/get-accel-sessions.sh", $lines);
foreach ($lines as $line) {
    if (strstr($line, " ppp")) {
        $fields = explode("|", $line);
        $usernames[] = trim($fields[1]);
    }
}

$uname_sql = "";
foreach ($usernames as $uname) {
    $uname_sql.="\"{$uname}\", ";
}

$sql = "select * from users";
$turnoff = array();

addmessage("Get users from mysql");
mysql_connect($mysql_host, $mysql_username, $mysql_username) or die("Cannot connect to mysql DB!\n");
mysql_select_db($mysql_db) or die("Cannot select DB!\n");
$mysqlr = mysql_query($sql) or die("BAD SQL: {$sql}\n");
while ($row_array = mysql_fetch_array($mysqlr)) {
    $turnoff[] = $row_array["pass1"];
}
mysql_close();
if (empty($turnoff)) {
    addmessage("List empty!");
    die;
    }
//print $sql;
$cnt=0;
foreach ($turnoff as $to) {
    addmessage("Turning off user {$to}");
    exec("accel-command.sh \"terminate match username {$to} soft\"");
    $cnt++;
}
addmessage("Turned off {$cnt} users!");
?>
